package com.wlcorp.forcazamis.sys.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("unchecked")
public class Pagination {

	public static String PAGE_URL = "pageURL";

	public static String PAGE_ATTRIBUTE = "page";

	public int PAGE_RECORD_COUNT = 15;

	public static final int PAGE_RECORD_LINE_NUM = 15;
	
	public static final int PAGE_JUMP_NUM = 5;
	
	private String getURL(HttpServletRequest request) {
		StringBuffer strbuf = new StringBuffer();
		strbuf.append("<form method=\"post\" name=\"_page_form\" action=\"").append(
				request.getRequestURI()).append("\">");
		strbuf.append("<input type=\"hidden\" name=\"page\"\\>");
		Enumeration emParams = request.getParameterNames();
		String sParam, sValue;
		while (emParams.hasMoreElements()) {
			sParam = (String) emParams.nextElement();
			sValue = request.getParameter(sParam);
			if (sValue != null && sValue.trim().length() != 0
					&& !sParam.equals("page") && !sParam.equals("p")
					&& !sParam.equals("action")) {
				strbuf.append("<input type=\"hidden\" name=\"").append(sParam)
						.append("\" value=\"").append(sValue).append("\"\\>");
			}
		}
		strbuf.append("</form>");
		String str = "<script language=javascript event=\"onload\" for=\"window\">\n";
		str += "var div = document.createElement(\"div\");\n";
		str += "document.body.appendChild(div);\n";
		str += "div.innerHTML = '" + strbuf.toString() + "';\n";
		str += "</script>\n";
		return str;
	}


	private int getPageNum(HttpServletRequest request) {
		String pageStr = request.getParameter(PAGE_ATTRIBUTE);
		int pageNum = 1;
		if (pageStr == null) {
			pageNum = 1;
		} else {
			pageNum = Integer.parseInt(pageStr);
			if (pageNum < 1)
				pageNum = 1;
		}
		return pageNum;
	}

	/**
	 * 
	 * @param request
	 * @param rowCount 总条数
	 * @param pageId   页数
	 */
	public void generatePageJSPFragment(HttpServletRequest request,
			int rowCount) {
		String pageURL = pagination(request, rowCount);
		request.setAttribute(PAGE_URL, pageURL);
	}

	public void generateFrontPageJSPFragment(HttpServletRequest request,
			int rowCount) {
		String pageURL = frontPagination(request, rowCount);
		request.setAttribute(PAGE_URL, pageURL);
	}
	
	public String pagination(HttpServletRequest request, int rowCount) {
		int pageId = getPageNum(request);
		String pageURL = getURL(request);
		StringBuffer strBuf = new StringBuffer();
		if (rowCount == 0) {
			strBuf.append("<font color=\"red\">无记录</font>");
			pageURL = strBuf.toString();
		} else {
			int iPageNum = ((int) (rowCount - 1) / PAGE_RECORD_COUNT)+1;
			if (iPageNum > 0) {
				if (pageId > 0) {
					strBuf.append("<a href='javascript:jumpPage(1)'><font color='#0000FF'>首页</font></a> ");
					strBuf.append("<a href='javascript:jumpPage("+ (pageId-1) + ")'><font color='#0000FF'>上页</font></a> ");
					int k = jumpLine(pageId);
					for(int i=k;i<=iPageNum;i++){
						if(i==0) i+=1;
						strBuf.append("<a href='javascript:jumpPage("+ (i) + ")'>") ;
								if(i==pageId)
									strBuf.append("<font color=red>");
								else
									strBuf.append("<font color='#0000FF'>");
								strBuf.append(i+"</font></a> ");
						if(i-k==10){
							break;
						}
					}
				} else {
					strBuf.append(" <font color='#828282'>首页 上页</font> ");
				}
				
				if (pageId < iPageNum) {
					strBuf.append(" <a href='javascript:jumpPage(" + (pageId+1)
							+ ")'><font color='#0000FF'>下页</font></a> ");
					strBuf.append("<a href='javascript:jumpPage(" + (iPageNum)
							+ ")'><font color='#0000FF'>尾页</font></a> ");
				} else {
					strBuf.append(" <font color='#828282'>下页</font> ");
					strBuf.append(" <font color='#828282'>尾页</font> ");
				}
			}
			int endRow = (pageId * PAGE_RECORD_COUNT >= rowCount) ? rowCount
					: pageId * PAGE_RECORD_COUNT;
			strBuf.append(" 第 " + ((pageId - 1) * PAGE_RECORD_COUNT + 1) + "到"
					+ endRow + "条/共" + rowCount + "条 第" + pageId + "页/共"
					+ iPageNum + "页");
			pageURL += strBuf.toString();
		}
		return pageURL;
	}
	
	public String frontPagination(HttpServletRequest request, int rowCount) {
		int pageId = getPageNum(request);
		StringBuffer pageURL = new StringBuffer(request.getRequestURI()).append("?a=1");
		StringBuffer strBuf = new StringBuffer();
		String sParam, sValue;
		Enumeration emParams = request.getParameterNames();
		while (emParams.hasMoreElements()) {
			sParam = (String) emParams.nextElement();
			sValue = request.getParameter(sParam);
			if (sValue != null && sValue.trim().length() != 0
					&& !sParam.equals("page") && !sParam.equals("p")
					&& !sParam.equals("action")) {
				try {
					pageURL.append("&").append(sParam).append("=").append(URLEncoder.encode(sValue,"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		if (rowCount == 0) {
			strBuf.append("<font color=\"red\">无记录</font>");
		} else {
			int iPageNum = ((int) (rowCount - 1) / PAGE_RECORD_COUNT)+1;
			if (iPageNum > 0) {
				if (pageId > 0) {
					strBuf.append("<a href='").append(pageURL).append("&page=1").append("'><font color='#0000FF'>首页</font></a> ");
					strBuf.append("<a href='").append(pageURL).append("&page=").append(pageId-1).append("'><font color='#0000FF'>上页</font></a> ");
					int k = jumpLine(pageId);
					for(int i=k;i<=iPageNum;i++){
						if(i==0) i+=1;
						strBuf.append("<a href='").append(pageURL).append("&page=").append(i).append("'>") ;
								if(i==pageId)
									strBuf.append("<font color=red>");
								else
									strBuf.append("<font color='#0000FF'>");
								strBuf.append(i+"</font></a> ");
						if(i-k==10){
							break;
						}
					}
				} else {
					strBuf.append(" <font color='#828282'>首页 上页</font> ");
				}
				
				if (pageId < iPageNum) {
					strBuf.append(" <a href='").append(pageURL).append("&page=").append(pageId+1).append("'><font color='#0000FF'>下页</font></a> ");
					/*strBuf.append("<a href='javascript:jumpPage(" + (iPageNum)
							+ ")'><font color='#0000FF'>尾页</font></a> ");*/
				} else {
					strBuf.append(" <font color='#828282'>下页</font> ");
					//strBuf.append(" <font color='#828282'>下页 尾页</font> ");
				}
			}
			//int endRow = (pageId * PAGE_RECORD_COUNT >= rowCount) ? rowCount
			//		: pageId * PAGE_RECORD_COUNT;
			//strBuf.append(" 第 " + ((pageId - 1) * PAGE_RECORD_COUNT + 1) + "到"
			//		+ endRow + "条/共" + rowCount + "条 第" + pageId + "页/共"
			//		+ iPageNum + "页");
		}
		return strBuf.toString();
	}
	
	private static int jumpLine(int pageid){
		int i = pageid/PAGE_JUMP_NUM;
		return i*PAGE_JUMP_NUM;
	}
	
	public void setPageCount(int count){
		this.PAGE_RECORD_COUNT = count;
	}
	
	public int getPageCount() {
		return this.PAGE_RECORD_COUNT;
	}
}
